package com.study.hystrix.simplespringcloudhystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.study.hystrix.simplespringcloudhystrix.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HystrixController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/movie/{id}")
    @HystrixCommand(fallbackMethod = "fallbackMethod",commandProperties = @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE"))
    public User findById(@PathVariable Long id){
        return restTemplate.getForObject("http://simple-provider-user/simple/" + id,User.class);
    }

    public User fallbackMethod(Long id){
        User user = new User();
        user.setId(id);
        return user;
    }
}
